# --------------------------------------------------------
# Buiilding portfolio 
# --------------------------------------------------------
# Written: Gianrocco Mucedero 
# IUSS Pavia/EUCENTRE/
# Created: 7 10 2019
# Units MUST be in kN, m 

wipe;									# Command used to start over without having to exit and restart the interpreter
puts "Model Generation Started";			# Text seen on interpreter
set outsdir "Result"
file mkdir $outsdir
file mkdir $outsdir/props

# --------------------------------------
# LOAD SOME SCRIPTS
# --------------------------------------
source Units.tcl
source joint_behaviour.tcl
source infill.tcl
source rcBC_nonDuct.tcl

# DEFINE MODEL DIMENSIONS AND DOFS
# --------------------------------------
model basic -ndm 2 -ndf 3
# --------------------------------------
# DEFINE SOME VARIABLES
# --------------------------------------
set cv		[expr 20*$mm];			# Cover
set dbL		[expr 14*$mm];			# Long Bars
set dbV		[expr 8*$mm];			# Trans Bars
set fyL		350.00;
set fuL		[expr 1.4*$fyL];
set fyV		350.00;
set Es		200000.;
set fc		20.00;
set Ec		[expr 3320*sqrt($fc)+6900];
set conc 	[list $fc $Ec $cv];
set brs 	[list $dbL $dbV]; 			# bars		Bar dias (dbL dbV) m
set STb		1;
set STc		1;
set H       3.00; #[m]
set Lcamp1   4.00
set Lcamp2   4.00

node 11   0.00 0.00 -mass 0.0 0.0 0.0; 
node 12   4.00 0.00 -mass 0.0 0.0 0.0; 
node 13   8.00 0.00 -mass 0.0 0.0 0.0; 
node 14   12.00 0.00 -mass 0.0 0.0 0.0; 

fix 11 1  1  1   
fix 12 1  1  1 
fix 13 1  1  1  
fix 14 1  1  1  


set hb1 [expr 0.40*0.5]
set hc1 [expr 0.30*0.5]

geomTransf PDelta	1 -jntOffset 0.0 $hb1 0.0 -$hb1; # z is in -X
geomTransf Linear 	3 -jntOffset $hc1 0.0 -$hc1 0.0; # # z is in Y

set GTcol1 	1
set GTbmx1 	3




#Column 
set rho_shr1 		0.002234021
set rho_top1zz1 	0.003665191
set rho_mid1zz1 	0.00
set rho_bot1zz1 	0.003665191
set rho_top2zz1 	0.003665191
set rho_mid2zz1 	0.00
set rho_bot2zz1		0.003665191
set rho_top1yy1 	0.003665191
set rho_mid1yy1 	0.00
set rho_bot1yy1 	0.003665191
set rho_top2yy1 	0.003665191
set rho_mid2yy1 	0.00
set rho_bot2yy1		0.003665191
#Beam1
set rho_shr2 		0.002234021
set rho_top1zz2 	0.002700667
set rho_mid1zz2 	0.00
set rho_bot1zz2 	0.002700667
set rho_top2zz2 	0.005401335
set rho_mid2zz2 	0.00
set rho_bot2zz2		0.002700667
set rho_top1yy2 	0.002700667
set rho_mid1yy2 	0.00
set rho_bot1yy2 	0.002700667
set rho_top2yy2 	0.005401335
set rho_mid2yy2 	0.00
set rho_bot2yy2		0.002700667




#Beam2
set rho_shr3 		0.002234021
set rho_top1zz3 	0.002700667
set rho_mid1zz3 	0.00
set rho_bot1zz3 	0.002700667
set rho_top2zz3 	0.002700667
set rho_mid2zz3 	0.00
set rho_bot2zz3		0.002700667
set rho_top1yy3 	0.002700667
set rho_mid1yy3 	0.00
set rho_bot1yy3 	0.002700667
set rho_top2yy3 	0.002700667
set rho_mid2yy3 	0.00
set rho_bot2yy3		0.002700667
#Beam4

set rho_shr4 		0.002234021
set rho_top1zz4 	0.004051001
set rho_mid1zz4 	0.00
set rho_bot1zz4 	0.002700667
set rho_top2zz4	    0.004051001
set rho_mid2zz4 	0.00
set rho_bot2zz4		0.002700667
set rho_top1yy4 	0.004051001
set rho_mid1yy4 	0.00
set rho_bot1yy4 	0.002700667
set rho_top2yy4 	0.004051001
set rho_mid2yy4 	0.00
set rho_bot2yy4     0.002700667

set pflag2 1

set pfile_joint [open $outsdir/props/joint.txt w];
set pfile_columns [open $outsdir/props/columns.txt w];
set pfile_beam [open $outsdir/props/beams.txt w];

joint_behaviour 2 21 0.00  3.00 300 300 400 300 $fc $cv $dbL $dbV 35.75 $H $pfile_joint
joint_behaviour 1 22 4.00  3.00 300 300 400 300 $fc $cv $dbL $dbV 71.50 $H $pfile_joint
joint_behaviour 1 23 8.00  3.00 300 300 400 300 $fc $cv $dbL $dbV 71.50 $H $pfile_joint
joint_behaviour 2 24 12.00  3.00 300 300 400 300 $fc $cv $dbL $dbV 35.75 $H $pfile_joint
set pfile_infill [open $outsdir/props/infill.txt w];

infill 555 "single" [list 121 122 12 11] 4000 3000 300 300 400 300 0.00 0.00 $Ec 2 1 0 1 4 1 $pfile_infill ;
infill 556 "single" [list 122 123 13 12] 4000 3000 300 300 400 300 0.00 0.00 $Ec 2 1 0 1 4 1 $pfile_infill ;
infill 557 "single" [list 123 124 14 13] 4000 3000 300 300 400 300 0.00 0.00 $Ec 2 1 0 1 4 1 $pfile_infill ;

close $pfile_infill
#Column element
rcBC_nonDuct $STc 521 $GTcol1 11 121 $fyL $fyV $Es $fc $Ec 0.300 0.300 0.180  $cv  $dbL $dbV  35.75   [expr 0.5*$H]  $rho_shr1 $rho_top1zz1 $rho_mid1zz1 $rho_bot1zz1 $rho_top2zz1 $rho_mid2zz1 $rho_bot2zz1 $rho_top1yy1 $rho_mid1yy1 $rho_bot1yy1 $rho_top2yy1 $rho_mid2yy1 $rho_bot2yy1 $pfile_columns $pflag2; 
rcBC_nonDuct $STc 522 $GTcol1 12 122 $fyL $fyV $Es $fc $Ec 0.300 0.300 0.180  $cv  $dbL $dbV  71.50   [expr 0.5*$H]  $rho_shr1 $rho_top1zz1 $rho_mid1zz1 $rho_bot1zz1 $rho_top2zz1 $rho_mid2zz1 $rho_bot2zz1 $rho_top1yy1 $rho_mid1yy1 $rho_bot1yy1 $rho_top2yy1 $rho_mid2yy1 $rho_bot2yy1 $pfile_columns $pflag2; 
rcBC_nonDuct $STc 523 $GTcol1 13 123 $fyL $fyV $Es $fc $Ec 0.300 0.300 0.180  $cv  $dbL $dbV  71.50   [expr 0.5*$H]  $rho_shr1 $rho_top1zz1 $rho_mid1zz1 $rho_bot1zz1 $rho_top2zz1 $rho_mid2zz1 $rho_bot2zz1 $rho_top1yy1 $rho_mid1yy1 $rho_bot1yy1 $rho_top2yy1 $rho_mid2yy1 $rho_bot2yy1 $pfile_columns $pflag2; 
rcBC_nonDuct $STc 524 $GTcol1 14 124 $fyL $fyV $Es $fc $Ec 0.300 0.300 0.180  $cv  $dbL $dbV  35.75   [expr 0.5*$H]  $rho_shr1 $rho_top1zz1 $rho_mid1zz1 $rho_bot1zz1 $rho_top2zz1 $rho_mid2zz1 $rho_bot2zz1 $rho_top1yy1 $rho_mid1yy1 $rho_bot1yy1 $rho_top2yy1 $rho_mid2yy1 $rho_bot2yy1 $pfile_columns $pflag2; 


#Beam element
rcBC_nonDuct $STb  721 $GTbmx1 621 622 $fyL $fyV $Es $fc $Ec 0.300 0.400 0.180  $cv  $dbL $dbV  0.00  [expr 0.5*$Lcamp1]  $rho_shr3 $rho_top1zz3 $rho_mid1zz3 $rho_bot1zz3 $rho_top2zz3 $rho_mid2zz3 $rho_bot2zz3 $rho_top1yy3 $rho_mid1yy3 $rho_bot1yy3 $rho_top2yy3 $rho_mid2yy3 $rho_bot2yy3 $pfile_beam $pflag2; 
rcBC_nonDuct $STb  722 $GTbmx1 622 623 $fyL $fyV $Es $fc $Ec 0.300 0.400 0.180  $cv  $dbL $dbV  0.00  [expr 0.5*$Lcamp2]  $rho_shr3 $rho_top1zz3 $rho_mid1zz3 $rho_bot1zz3 $rho_top2zz3 $rho_mid2zz3 $rho_bot2zz3 $rho_top1yy3 $rho_mid1yy3 $rho_bot1yy3 $rho_top2yy3 $rho_mid2yy3 $rho_bot2yy3 $pfile_beam $pflag2;
rcBC_nonDuct $STb  723 $GTbmx1 623 624 $fyL $fyV $Es $fc $Ec 0.300 0.400 0.180  $cv  $dbL $dbV  0.00  [expr 0.5*$Lcamp2]  $rho_shr3 $rho_top1zz3 $rho_mid1zz3 $rho_bot1zz3 $rho_top2zz3 $rho_mid2zz3 $rho_bot2zz3 $rho_top1yy3 $rho_mid1yy3 $rho_bot1yy3 $rho_top2yy3 $rho_mid2yy3 $rho_bot2yy3 $pfile_beam $pflag2; 

close $pfile_columns
close $pfile_beam
close $pfile_joint


#set w_piano -23.29
#set w_roof  -13.85
set self_pilastri -2.16
set self_travi -2.88

# Modal Analysis
set numModes 1
for { set k 1 } { $k <= $numModes } { incr k } {
    recorder Node -file [format "modes/mode%i.out" $k] -nodeRange 1 6 -dof 1 2 3  "eigen $k"
} 
set lambda [eigen  $numModes];
set omega {}
set f {}
set T {}
set pi 3.141593
foreach lam $lambda {
	lappend omega [expr sqrt($lam)]
	lappend f [expr sqrt($lam)/(2*$pi)]
	lappend T [expr (2*$pi)/sqrt($lam)]
}
puts "Modal Analysis Completed..........."

puts [format "Period s :
Period= %.3f" $T]
set period [open $outsdir/period.txt w];
puts $period [format "%.5f" $T]
close $period
# Load 	
#set w_piano -23.29
#set w_roof  -13.85
set self_pilastri -2.16
set self_travi -2.88
#8.2875 4.8075

pattern Plain 101 Constant {
	############# ELEVATION 2 #############
	load 121  	0.0 -35.75   0.0
	load 122  	0.0 -71.50   0.0
	load 123  	0.0 -71.50    0.0
	load 124  	0.0 -35.75   0.0
eleLoad -ele 521 522 523 524 -type -beamUniform  0.0 $self_pilastri 0.0
}
# Apply the load
# 	constraints Transformation
constraints 	Penalty 1e14 1e14
numberer 		RCM
system UmfPack -lvalueFact 30
# system UmfPack Mumps
test EnergyIncr	1e-6 1000
algorithm KrylovNewton
set nG 500;
integrator LoadControl [expr 1/$nG]
analysis Static
analyze $nG
# maintain constant gravity loads and reset time to zero
loadConst -time 0.0
puts "Gravity Analysis Completed..........."
print ele 521 522 523 524

puts "Model Built"


puts "Load Pattern Started";				# Text seen on interpreter
# Load Pattern Definition--------------------------------------------------------
pattern Plain 1 Linear { 
load 121 	 1.0 0.0 0.0;	
load 122 	 1.0 0.0 0.0;		
load 123 	 1.0 0.0 0.0;	
load 124 	 1.0 0.0 0.0;	
}
puts "Recorders Started";					# Text seen on interpreter
recorder Node -file ID_5_disp1_infill2.out -node 121 -dof 1 disp
# Support reactions
recorder Node -file ID_5_V1_infill2.out -node 11 12 13 14 -dof 1 reaction;
puts "Analysis Objects Started";			# Text seen on interpreter
constraints Plain;
numberer Plain;								# Nodes are assigned dof's arbitrarily
system BandGeneral;							# Un-symmetric banded system solved using Lapack Band General Solver
test NormDispIncr 1.0e-3 600 0;				# Create the convergence test
algorithm Newton;							# Create the solution algorithm
# integrator DisplacementControl $nodeTag $dofTag $dU1
integrator DisplacementControl 121 1 0.0001
analysis Static;							# Static Analysis
analyze 800000;									
puts "Done with Experimental test";		# Text seen on interpreter
wipe;

